package net.inxweb.bluemeerkat.web.rest.service.glossary.attribute;

import javax.annotation.Resource;

import net.inxweb.bluemeerkat.domain.model.glossary.Attribute;
import net.inxweb.bluemeerkat.domain.model.glossary.GlossaryType;
import net.inxweb.bluemeerkat.domain.service.GlossaryService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/glossary/attribute") 
public class AttributeContorller {
	@Resource(name="glossaryServiceImpl")
	private GlossaryService glossaryService;
	
	@RequestMapping(value="/create", method=RequestMethod.POST) 
    public ModelAndView requestCreate(
    		@RequestParam(value="glossaryTypeId") Long glossaryTypeId,
    		@RequestParam(value="attributeName") String attributeName) {  
		
		GlossaryType glossaryType = this.glossaryService.findGlossaryType(glossaryTypeId); 
		glossaryType.addAttribute(new Attribute(attributeName)); 
		this.glossaryService.modifyGlossaryType(glossaryType);
		
		return new ModelAndView("redirect:/glossary/type/modifyForm.do")
		.addObject("glossaryTypeId", glossaryType.getId())
		.addObject("message", "creating is successful");
    }  
	
	@RequestMapping(value="/modify", method=RequestMethod.POST) 
	public ModelAndView requestModify(
			@RequestParam(value="glossaryTypeId") Long glossaryTypeId,
			@RequestParam(value="attributeId") Long attributeId,
			@RequestParam(value="attributeName") String attributeName) {  
		
		GlossaryType glossaryType = this.glossaryService.findGlossaryType(glossaryTypeId);
		
		Attribute attribute = glossaryType.getAttribute(attributeId);
		attribute.setName(attributeName); 
		
		this.glossaryService.modifyGlossaryType(glossaryType);
		
		return new ModelAndView("redirect:/glossary/type/modify.do")
		.addObject("glossaryTypeId", glossaryType.getId())
		.addObject("message", "creating is successful");
	}  
}
